package com.iot.ammeter.controller.campus;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.iot.ammeter.anno.LogAnnotation;
import com.iot.ammeter.dto.web.campus.CampusInfoDTO;
import com.iot.ammeter.dto.web.campus.SchoolNameAndCampusNameDTO;
import com.iot.ammeter.entity.web.campus.Campus;
import com.iot.ammeter.query.web.campus.CampusQuery;
import com.iot.ammeter.responseResult.ResponseResult;
import com.iot.ammeter.service.web.campus.CampusService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 校区 前端控制器
 * </p>
 *
 * @author basil
 * @since 2021-09-25
 */
@Api(tags = {"校区管理"})
@RestController
@RequestMapping("/api/campus")
public class CampusController {

    private final CampusService campusService;

    public CampusController(CampusService campusService) {
        this.campusService = campusService;
    }

    /**
     * 获取校区及校区名
     *
     * @return 返回树形校区及校区名json数据
     */
    @ApiOperation("获取校区及校区名")
    @LogAnnotation(module = "getSchoolNameAndCampusName", operator = "获取校区及校区名")
    @GetMapping("/school_name_campus_name")
    public ResponseResult getSchoolNameAndCampusName() {
        Map<String, List<SchoolNameAndCampusNameDTO>> map = campusService.getSchoolNameAndCampusName();
        return map.size() > 0 ? ResponseResult.SUCCESS(map) : ResponseResult.FAILED("无数据");
    }


    /**
     * 获取学校信息列表
     *
     * @param page        请求页码
     * @param pageSize    每页数量
     * @param campusQuery 查询条件
     * @return 返回学校信息列表json数据
     */
    @ApiOperation("获取学校信息列表")
    @LogAnnotation(module = "getCampusList", operator = "获取学校信息列表")
    @PostMapping("/list/{page}/{pageSize}")
    public ResponseResult getCampusList(@PathVariable int page,
                                        @PathVariable int pageSize,
                                        @RequestBody(required = false) CampusQuery campusQuery) {

        if (page <= 0 || pageSize <= 0) {
            return ResponseResult.FAILED("页码参数错误");
        }
        IPage<Campus> campusList = campusService.getCampusList(page, pageSize, campusQuery);
        return ResponseResult.SUCCESS(campusList);
    }

    /**
     * 添加校区信息
     *
     * @param campusInfoDTO 校区信息
     * @return 添加成功或者失败提示json数据
     */
    @ApiOperation("添加校区信息")
    @LogAnnotation(module = "addCampus", operator = "添加校区信息")
    @PostMapping("/adjuration")
    public ResponseResult addCampus(@RequestBody CampusInfoDTO campusInfoDTO) {
        String tips = campusService.addCampus(campusInfoDTO);
        return "添加校区成功".equals(tips) ? ResponseResult.SUCCESS(tips) : ResponseResult.FAILED(tips);
    }

    /**
     * 编辑/更新校区信息
     *
     * @param campusId      校区id
     * @param campusInfoDTO 校区信息
     * @return 编辑成功或者编辑失败提示json数据
     */
    @ApiOperation("编辑/更新校区信息")
    @LogAnnotation(module = "editCampus", operator = "编辑/更新校区信息")
    @PutMapping("/update/{campusId}")
    public ResponseResult editCampus(@PathVariable String campusId, @RequestBody CampusInfoDTO campusInfoDTO) {
        String tips = campusService.editCampus(campusId, campusInfoDTO);
        return "更新校区成功".equals(tips) ? ResponseResult.SUCCESS(tips) : ResponseResult.FAILED(tips);
    }

    /**
     * 删除校区信息（逻辑删除）
     *
     * @param campusId 校区id
     * @return 删除成功或者删除失败json数据
     */
    @ApiOperation("删除校区信息")
    @LogAnnotation(module = "delCampus", operator = "删除校区信息")
    @DeleteMapping("/delete")
    public ResponseResult delCampus(@RequestBody String[] campusId) {
        String tips = campusService.delCampus(campusId);
        return "校区删除失败".equals(tips) ? ResponseResult.FAILED(tips) : ResponseResult.SUCCESS(tips);
    }

    /**
     * 获取校区详细信息
     *
     * @param campusId 校区id
     * @return 返回校区详细信息json数据json数据
     */
    @ApiOperation("获取校区详细信息")
    @LogAnnotation(module = "getCampusDetailsById", operator = "获取校区详细信息")
    @GetMapping("/{campusId}")
    public ResponseResult getCampusDetailsById(@PathVariable String campusId) {
        if (StringUtils.isEmpty(campusId)) {
            return ResponseResult.FAILED("校区id不能为空");
        }
        CampusInfoDTO campusDetails = campusService.getCampusDetails(campusId);
        return campusDetails == null ? ResponseResult.FAILED("校区不存在") : ResponseResult.SUCCESS(campusDetails);
    }
}
